package com.example.doctor.mapper;

import com.example.doctor.dto.resp.Client_FollowrRemind;
import com.example.doctor.dto.resp.NotesRsp;
import com.example.doctor.dto.resp.QaListRsp;
import com.example.doctor.dto.resp.TakePartListRsp;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author sifan
 * @PackagName:com.example.doctor.mapper
 * @ClassName: QaMapper
 * @Description:
 * @date:2021/1/26 13:03
 */
@Component
public interface QaMapper {
    @Select("with list as "
    +" (select a.tid_bigint FROM h_question_main a WHERE a.dc_id_bigint=0 OR a.dc_id_bigint=#{dcId})"
    +" ,qcount as "
    +" (select b.tid_bigint,count(a.q_id_bigint) as qCount from list b"
    +" left join h_question a on a.t_id_bigint = b.tid_bigint"
    +" group by b.tid_bigint)"
    +" SELECT a.tid_bigint AS TID,a.dc_id_bigint AS DCID,a.t_name_varchar AS TName,a.create_time_datetime AS CreateTime,b.qCount"
    +" FROM list l"
    +" inner join h_question_main a on l.tid_bigint = a.tid_bigint"
    +" inner join qcount b on l.tid_bigint = b.tid_bigint"
            + " limit #{skip},#{limit}")
    List<QaListRsp> list(@Param("dcId") Long dcId,@Param("skip") Integer skip,@Param("limit") Integer limit);

    @Select("with list as "
            +" (select a.tid_bigint FROM h_question_main a WHERE a.dc_id_bigint=0 OR a.dc_id_bigint=#{dcId})"
            +" ,qcount as "
            +" (select b.tid_bigint,count(a.q_id_bigint) as qCount from list b"
            +" left join h_question a on a.t_id_bigint = b.tid_bigint"
            +" group by b.tid_bigint)"
            +" SELECT count(*)"
            +" FROM list l"
            +" inner join h_question_main a on l.tid_bigint = a.tid_bigint"
            +" inner join qcount b on l.tid_bigint = b.tid_bigint")
    long count(@Param("dcId") Long dcId);

    @Select("SELECT a.a_Item_nvarchar AS aitem FROM h_answer a WHERE q_id_bigint =#{qId}")
    String[] queryByQid(@Param("qId") Long qId);

    @Select("SELECT f_code_nvarchar FROM followr_question_main WHERE dc_id_bigint=#{dcId} AND answer_bit=TRUE  GROUP BY f_code_nvarchar ")
    List<String> groupFcodes(@Param("dcId") Long dcId);

    @Select("SELECT f_code_nvarchar FROM followr_question_main WHERE dc_id_bigint=#{dcId} AND answer_bit=TRUE  GROUP BY f_code_nvarchar")
    List<String> countGroupFcodes(@Param("dcId") Long dcId);

    @Select("SELECT a.create_time_datetime AS CreateTime,a.f_code_nvarchar" +
            " AS FCode,a.send_type_tinyint AS SendType,a.t_name_bvarchar AS " +
            "TName FROM followr_question_main a WHERE a.f_code_nvarchar=#{fCode}")
    List<NotesRsp> findByFcode(@Param("fCode") String fCode);
    @Select("SELECT a.fid_bigint AS FID,a.finish_time_datetime AS FinishTime,b.name_nvarchar " +
            "AS Name ,b.sex_nvarchar AS Sex , a.t_name_bvarchar AS T" +
            "Name,b.birthday_date AS birthday FROM followr_question_main a INNER JOIN " +
            "client_patients_info b ON(a.pid_bigint=b.pid_bigint) WHERE a.dc_id_bigint=#{dCId} " +
            "AND a.answer_bit=TRUE AND a.f_code_nvarchar=#{fCode}")
    List<TakePartListRsp> findByFcodeTake(@Param("dCId") Long dCId ,@Param("fCode") String fCode);

    @Select("SELECT a.pid_bigint AS pids FROM rel_dr_and_patients a INNER JOIN client_patients_info b " +
            "ON(a.pid_bigint=b.pid_bigint) WHERE a.dc_id_bigint=#{dcId} AND disable_bit =FALSE")
    List<String> queryPidByDcId(@Param("dcId") Long dcId);

    @Select("SELECT c.open_id_nvarchar AS OpenID,b.name_nvarchar AS Name,a.create_time_datetime " +
            "AS CreateTime,a.t_name_bvarchar AS TName,a.dc_id_bigint AS DCID " +
            "FROM followr_question_main a INNER JOIN client_patients_info b " +
            "ON(a.pid_bigint=b.pid_bigint) INNER JOIN client_base_info c ON(b.client_id_bigint=c.uid_bigint)" +
            " WHERE a.f_code_nvarchar=#{fcode} AND b.disable_bit =FALSE")
    List<Client_FollowrRemind> Client_FollowrRemind(@Param("fcode") String fcode);
}
